﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TTTBoard
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            RecalcBoard();
        }


        private int cellHeight;
        private int cellWidth;


        private void RecalcBoard()
        {
            this.cellHeight = pictureBox1.Height / 3;
            this.cellWidth = pictureBox1.Width / 3;
            pictureBox1.Refresh();
        }


        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawLine(Pens.Blue, 0, cellHeight, pictureBox1.Width, cellHeight);
            e.Graphics.DrawLine(Pens.Blue, 0, 2 * cellHeight, pictureBox1.Width, 2 * cellHeight);

            e.Graphics.DrawLine(Pens.Blue, cellWidth, 0, cellWidth, pictureBox1.Height);
            e.Graphics.DrawLine(Pens.Blue, 2*cellWidth, 0, 2*cellWidth, pictureBox1.Height);

                
            DrawToken(e.Graphics, 'X', 0,0);
            DrawToken(e.Graphics, 'O', 1, 1);
        }


        private void pictureBox1_Resize(object sender, EventArgs e)
        {
            RecalcBoard();
        }


        private void pictureBox1_Click(object sender, EventArgs e)
        {
            var ce = (MouseEventArgs)e;
            
            var col = ce.X  / this.cellWidth;
            var row = ce.Y/this.cellHeight;

            DrawToken(pictureBox1.CreateGraphics(), '%', col, row);
        }

        
        private void DrawToken(Graphics g, char token, int col, int row)
        {
            using (var f = new Font("Arial", (int)(cellHeight * 0.9)))
            {
                var chSize = g.MeasureString(token.ToString(), f);
                g.DrawString(token.ToString(), f, Brushes.DarkBlue, col * this.cellWidth + (this.cellWidth - chSize.Width) / 2, row * this.cellHeight + (this.cellHeight - chSize.Height) / 2 + (int)(chSize.Height * 0.06));
            }
        }
    }
}
